package cn.com.cifi.mars.util;

import cn.com.cifi.mars.bean.vo.returnPlan.returnReport.ReturnPlanProgressVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 *
 * 类名称：ReturnPlanProgressTemplate
 * 类描述：回款计划进度表模板
 * @version 1.0.0
 */
@Slf4j
public class ReturnPlanProgressTemplate {
    private static final String TEMPLATE_PATH = "/template/return_plan_progress_template.xlsx";

    private XSSFWorkbook workbook;

    private XSSFSheet sheet;

    //默认sheet索引
    private int defaultSheetIndex = 0;
    //数据开始行
    private int dataBeginRow = 1;
    //当前行标
    private int currentDataRowCursor = dataBeginRow;
    //单元格样式
    private XSSFCellStyle cellStyle;
    //文字样式
    private XSSFCellStyle cellStyle1;


    public ReturnPlanProgressTemplate(String name) {

        try {
            InputStream tempInputStream = ReturnPlanProgressTemplate.class.getResourceAsStream(TEMPLATE_PATH);
            if (tempInputStream != null) {
                workbook = new XSSFWorkbook(tempInputStream);
                sheet = workbook.getSheetAt(defaultSheetIndex);
                setSheetDate(name);
                XSSFRow row = sheet.getRow(dataBeginRow);
                cellStyle1 = row.getCell(0).getCellStyle();

            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }


    public void setSheetDate(String name) {
        workbook.setSheetName(defaultSheetIndex, workbook.getSheetName(defaultSheetIndex).replace("回款计划进度表", name));
    }

    /**
     * 批量插入行
     * @param list 报表数据
     */
    public synchronized void appendRows(List<ReturnPlanProgressVo> list) {
        for (ReturnPlanProgressVo progress : list) {
            this.appendRow(progress);
        }
    }

    /**
     * 插入行
     *
     * @param
     */
    public synchronized void appendRow(ReturnPlanProgressVo progress) {
        XSSFRow row = sheet.getRow(currentDataRowCursor);
        if (row == null) {
            row = sheet.createRow(currentDataRowCursor);
        }
        int index = 0;
        //项目信息
        cellStyle = cellStyle1;
        this.appendCell(row, index++,progress.getBusinessDivisionName());
        this.appendCell(row, index++,progress.getCityCompanyName());
        this.appendCell(row, index++,progress.getProjectCode());
        this.appendCell(row, index++,progress.getProjectName());
        this.appendCell(row, index++,progress.getStageCode());
        this.appendCell(row, index++,progress.getStageName());
        this.appendCell(row, index++,progress.getReturnPlanVersionName());
        this.appendCell(row, index++,progress.getVersionStateName());
        this.appendCell(row, index++,progress.getCreateByName());
        this.appendCell(row, index++,null == progress.getCreateTime() ? "" : LocalDateTimeUtils.formatTime(progress.getCreateTime(),"yyyy/MM/dd HH:mm"));
        this.appendCell(row, index++,progress.getVersionCreateName());
        this.appendCell(row, index++,null == progress.getSuccessApprovalTime() ? "" : LocalDateTimeUtils.formatTime(progress.getSuccessApprovalTime(),"yyyy/MM/dd HH:mm"));
        this.appendCell(row, index++,progress.getSourceSignVersionName());

        currentDataRowCursor++;

    }

    /**
     * 插入列
     *
     * @param row
     * @param index
     * @param value
     */
    public synchronized void appendCell(XSSFRow row, int index, String value) {
        XSSFCell cell = row.getCell(index);
        if (cell == null) {
            cell = row.createCell(index);
        }
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value == null ? "" : value);
    }

    /**
     * 插入列
     *
     * @param row
     * @param index
     * @param value
     */
    public synchronized void appendCell(XSSFRow row, int index, Long value) {
        XSSFCell cell = row.getCell(index);
        if (cell == null) {
            cell = row.createCell(index);
        }
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value);
    }

    /**
     * 导出
     * @param outputStream
     * @throws IOException
     */
    public void write(OutputStream outputStream) throws IOException {
        workbook.write(outputStream);
    }
}
